package com.aode.music.view.fragment.mainfragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aode.common.bean.request.Request;
import com.aode.common.bean.request.SearchData;
import com.aode.common.bean.response.Mllist;
import com.aode.common.bean.response.Music;
import com.aode.common.url.UrlGather;
import com.aode.downloadmusic.view.activity.DownloadActivity;
import com.aode.music.R;
import com.aode.music.adapter.home.HomeMusicAdapter;
import com.aode.music.adapter.home.HomePlayListAdapter;
import com.aode.music.databinding.FragmentHomeBinding;
import com.aode.music.ui.imageLoader;
import com.aode.music.viewmodel.HomeViewModel;
import com.aode.playlist.view.activity.HomePlayListActivity;
import com.aode.playlist.view.activity.PlayListActivity;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HomeFragment extends Fragment implements HomePlayListAdapter.PlayListInterface {
    private FragmentHomeBinding homeBinding;
    //指定图片地址
    private List<String> imagePaths;
    //指定轮播图每个图片的title
    private List<String> imageTitles;
    private List<Music> musicList;
    private List<Mllist> mllistList;
    private HomeViewModel homeViewModel;
    private SearchData searchData = new SearchData();
    private HomePlayListAdapter playListAdapter;
    private HomeMusicAdapter musicAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        homeBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_home,container,false);
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        homeBinding.setHomefragment(HomeFragment.this);
        initData();
        initView();
        initListener();
        return  homeBinding.getRoot();
    }
    public void click(View view){
        switch (view.getId()){
            case R.id.iv_home_collect:
                collcet();
                break;
            case R.id.iv_home_playlist:
                playlist();
                break;
            case R.id.iv_home_friend:
                friend();
                break;
            case R.id.iv_home_listener:
                listener();
                break;
            default:
                break;
        }
    }
    //听歌识曲界面
    private void listener() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), DownloadActivity.class);
        startActivity(intent);
    }

    //扩列界面
    private void friend() {

    }

    //歌单界面
    private void playlist() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), PlayListActivity.class);
        startActivity(intent);
    }

    //收藏界面
    private void collcet() {


    }



    private void initListener() {
        initMusicDataListener();
        initMlistDataListener();
    }

    private void initMlistDataListener() {
        homeViewModel.getMllistLiveData().observe(getViewLifecycleOwner(), pageResult -> {
            if(pageResult!=null){
                Log.e("TAG","------"+pageResult.getData().getRecords());
                if(pageResult.getCode().equals("0")&&pageResult.getData().getRecords().size()>0){
                    mllistList = pageResult.getData().getRecords();
                    initMllist((ArrayList<Mllist>)mllistList);
                }
            }


        });
    }

    private void initMusicDataListener() {
        homeViewModel.getMusicLiveData().observe(getViewLifecycleOwner(),pageResult -> {
            if(pageResult!=null){
                if(pageResult.getCode().equals("0")){
                    musicList = pageResult.getData().getRecords();
                    initMusic((ArrayList<Music>) musicList);
                }
            }

        });
    }

    private void initView() {
        initBanner();
    }



    private void initData() {
        initBannerData();
        initMllistData();
        initMusicData();

    }
    //歌曲数据
    private void initMusicData() {
        searchData.setPageSize(5);
        searchData.setPageNum(1);
        homeViewModel.getMusic(searchData);

    }


    //歌单数据
    private void initMllistData() {
        Random random = new Random();
        int a = random.nextInt(4)+1;
        searchData.setPageSize(6);
        searchData.setPageNum(a);
        homeViewModel.getMllist(searchData);
    }
    private void initMllist(ArrayList<Mllist> mllistArrayList) {
        if(mllistArrayList!=null&&!mllistArrayList.isEmpty()&&mllistArrayList.size()>0){
            playListAdapter = new HomePlayListAdapter(this.getContext(),mllistArrayList,this);
            homeBinding.homeRecyclerPlaylist.setLayoutManager(new GridLayoutManager(getActivity(),3));
            homeBinding.homeRecyclerPlaylist.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL));
            homeBinding.homeRecyclerPlaylist.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.HORIZONTAL));
            homeBinding.homeRecyclerPlaylist.setAdapter(playListAdapter);
        }
    }
    private void initMusic(ArrayList<Music> musicList){
        if(musicList!=null&&!musicList.isEmpty()&&musicList.size()>0){
            musicAdapter = new HomeMusicAdapter(this.getContext(),musicList);
            homeBinding.homeRecyclerMusic.setLayoutManager(new LinearLayoutManager(this.getContext(),LinearLayoutManager.VERTICAL,false));
            homeBinding.homeRecyclerMusic.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL));
            homeBinding.homeRecyclerMusic.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.HORIZONTAL));
            homeBinding.homeRecyclerMusic.setAdapter(musicAdapter);
        }
    }
    /**
     * 轮播图数据
     */
    private void initBanner() {
        homeBinding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        homeBinding.banner.setImageLoader(new imageLoader());
        homeBinding.banner.setBannerTitles(imageTitles);
        //指示器位置
        homeBinding.banner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片
        homeBinding.banner.setImages(imagePaths);
        homeBinding.banner.start();
    }

    private void initBannerData() {
        imagePaths = new ArrayList<>();
        imageTitles = new ArrayList<>();
        for(int i =0;i < 10;i++){
            imagePaths.add(UrlGather.BASE_URL+"/files/banner"+(i+1)+".jpg");
        }
        imageTitles.add("队长&黄礼格");
        imageTitles.add("王一博");
        imageTitles.add("一周上新盘点");
        imageTitles.add("繁星的诉说");
        imageTitles.add("长安行");
        imageTitles.add("黄旭/满舒克");
        imageTitles.add("潜力热单");
        imageTitles.add("日本宝藏乐队");
        imageTitles.add("是二智呀");
        imageTitles.add("人间四季");
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onItemClick(Mllist mllist) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), HomePlayListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("mllist",mllist);
        intent.putExtras(bundle);
        startActivityForResult(intent, Request.HOME_TO_PLAYLIST);
    }


}
